package com.example.printer2.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.printer2.FileActivity;
import com.example.printer2.PrinterActivity;
import com.example.printer2.R;
import com.example.printer2.po.Printer;

import java.util.List;
import java.util.Random;

public class PrinterAdapter extends BaseAdapter {
    private List<Printer> list;
    private Context context;
    public PrinterAdapter(List<Printer> list, Context context)
    {
        this.list=list;
        this.context=context;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class MyViewHolder
    {
        public TextView name;
        public TextView position;
        public TextView statue;
        public Button button;
    }

    private void getStatue(TextView textView,int id)
    {
        if(id==1)
        {
            textView.setText("空闲");
            textView.setTextColor(Color.rgb(0,128,0));
        }
        if(id==2)
        {
            textView.setText("忙碌中，预计等待5分钟");
        }
        if(id==3)
        {
            textView.setText("忙碌中，预计等待10分钟");
        }
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
            Printer printer = list.get(position);
            PrinterAdapter.MyViewHolder viewHolder = new PrinterAdapter.MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_printer, null);
            viewHolder.name = (TextView) convertView.findViewById(R.id.file_name);
            viewHolder.name.setText("打印机" + String.valueOf(printer.getId()));
            viewHolder.position = (TextView) convertView.findViewById(R.id.create_time);
            viewHolder.position.setText(printer.getPosition());
            viewHolder.statue = (TextView) convertView.findViewById(R.id.printer_statue);
            getStatue(viewHolder.statue,printer.getId());
            viewHolder.button = (Button) convertView.findViewById(R.id.download);

            View.OnClickListener onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                        Intent intent = new Intent(context, FileActivity.class);
                        intent.putExtra("id", printer.getId());
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                        context.startActivity(intent);
                }
            };
            convertView.setOnClickListener(onClickListener);
            viewHolder.button.setOnClickListener(onClickListener);
            convertView.setTag(viewHolder);
        return convertView;
    }
}
